import { AstContext } from "../../../context";
import { Pages } from "../Pages";
import { PageVisitor } from "../pageVisitor";
import { currentPage } from "./consts";
import { createToIndex } from "./createToIndex";
import { createToTitle } from "./createToTitle";
import { parseBlockTopAdjustView } from "./parseBlockTopAdjustView";
/**
 * visitHorizontalScrollView R.kcube_tab_strip
 * @param visitor 
 * @param ast 
 * @param ctx 
 */
export function parseTopBar(visitor: PageVisitor, ast: AutoJs.UiObject, ctx: AstContext) {
    if (!ast.visibleToUser()) return true;
    const hasparseTopBar = ctx.get<boolean>('parseTopBar')
    if (hasparseTopBar) {
        return parseBlockTopAdjustView(visitor, ast, ctx)
    }
    const child = ctx.bfsOne(ast, it => ctx.filterChildren(it, a => a.className() === `android.widget.FrameLayout`).length === 5)
    if (!child) return true;
    ctx.set('parseTopBar', true)
    ctx.set(`toTopTab`, createToIndex(child, ctx, `topTabIndex`))
    ctx.set(`toTopTabTitle`, createToTitle(child, ctx, `topTab`))
    ctx.eachChild(child, (node, index) => {
        const selected = node.selected()
        if (selected) {
            const text = ctx.getText(node)
            switch (text) {
                case `商城`:
                    ctx.set(currentPage, Pages.homeShop)
                    break;
                case `关注`:
                    ctx.set(currentPage, Pages.homeFollow)
                    break;
                case `发现`:
                    ctx.set(currentPage, Pages.homeFind)
                    break;
                case `同城`:
                    ctx.set(currentPage, Pages.homeCity)
                    break;
                case `直播`:
                    ctx.set(currentPage, Pages.homeLiveLive)
                    break;
            }
            ctx.set(`topTabIndex`, index)
            ctx.set(`topTab`, text)
        }
    })
    return true;
}